TWB.Modules={};
TWB.prefetch=[];
TWB.settings=[];
TWB.New_Module=function(module){
	for(prop in module){
		if(typeof module.name=="undefined"){
			continue;
		}
		if(typeof TWB.Modules[module.name]=="undefined"){
			TWB.Modules[module.name]={};
		}
		
		if(prop=="settings"){
			TWB.settings.push([module.name,module[prop]]);
		}
		if(prop=="init"){
			TWB.prefetch.push([module.priority,module.init]);
		}
		else{
			if(prop!="name" && prop!="priority"){
				TWB.Modules[module.name][prop]=module[prop];
			}
		}
	}
	return true;
};

TWB.Load_Modules=function(){
	// Read all files
	var loc=TWB.Cc["@mozilla.org/file/directory_service;1"].getService(TWB.Ci.nsIProperties).get("ProfD", TWB.Ci.nsIFile).path+TWB.SL+"extensions"+TWB.SL+"twbooster@aisolutions.biz"+TWB.SL+"chrome"+TWB.SL+"modules";
	netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	var f=TWB.Cc["@mozilla.org/file/local;1"].createInstance(TWB.Ci.nsILocalFile);
	f.initWithPath(loc);
	var fs=f.directoryEntries;
	while(fs.hasMoreElements()){
		var FILE=fs.getNext();
		var FILE_PATH=FILE.QueryInterface(Components.interfaces.nsILocalFile).path;
		if(!FILE_PATH.match(".js") || FILE_PATH.match("~") || FILE_PATH.indexOf(".js")!=FILE_PATH.length-3){
			continue;
		}
		try{
			TWB.jsLoader.loadSubScript("file:"+TWB.SL+TWB.SL+FILE_PATH, TWB);
		}catch(e){
			FILE_PATH=FILE_PATH.split(TWB.SL);
			TWB.log(e,"Modules::"+FILE_PATH[FILE_PATH.length-1].split(".")[0]);
		}
	}
	// Load Prefetch
	TWB.prefetch.sort();
	var it;
	for(it=0; it<TWB.prefetch.length; it++){
		try{
			TWB.prefetch[it][1]();
		}catch(e){
			TWB.log(e,":Modules:");
		}
	}
}

window.addEventListener("load",function(){ TWB.Load_Modules(); },false);